// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.commands;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.requests.CreateRequest;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DBTableCreateCommand extends AbstractGEFCommand {

    private final DatabaseDiagram diagram;

    /** True, if newShape was added to parent. */
    private boolean dbConnAdded;

    private final DBTable newDBTable;

    private final Dimension size;

    private final Point location;

    boolean isGenName = true;

    /**
     * Qiang.Zhang.Adolf@gmail.com DBTableCreateCommand constructor comment.
     * 
     * @param diagram
     * @param request
     */
    public DBTableCreateCommand(DatabaseDiagram diagram, CreateRequest request) {
        if (diagram == null || request == null || !(request.getNewObject() instanceof DBTable)) {
            throw new IllegalArgumentException();
        }
        this.diagram = diagram;

        newDBTable = (DBTable) request.getNewObject();
        size = request.getSize();
        location = request.getLocation();
        setLabel(Messages.getString("DBTableCreateCommand.Label")); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com DBTableCreateCommand constructor comment.
     * 
     * @param diagram
     * @param rectangle
     * @param newTable
     */
    public DBTableCreateCommand(DatabaseDiagram diagram, Rectangle rectangle, DBTable newTable) {
        this.diagram = diagram;
        this.location = rectangle.getLocation();
        this.size = rectangle.getSize();
        this.newDBTable = newTable;
        setLabel(Messages.getString("DBTableCreateCommand.Label")); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com DBTableCreateCommand constructor comment.
     * 
     * @param diagram
     * @param location
     * @param newTable
     * @param isGenName
     */
    public DBTableCreateCommand(DatabaseDiagram diagram, Rectangle rectangle, DBTable newTable, boolean isGenName) {
        this(diagram, rectangle, newTable);
        this.isGenName = isGenName;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canUndo()
     */
    @Override
    public boolean canUndo() {
        return dbConnAdded;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {

        if (size != null) {
            int i = size.height;
            newDBTable.setHeight(i > 80 ? i : 80);
            i = size.width;
            newDBTable.setWidth(i > 80 ? i : 80);
        } else {
            newDBTable.setHeight(180);
            newDBTable.setWidth(80);
        }
        newDBTable.setX(location.x);
        newDBTable.setY(location.y);
        if (isGenName) {
            newDBTable.setName(MetadataHelper.generateNewTableName(diagram, newDBTable.getName()));
        }
        dbConnAdded = diagram.getDbChildren().add(newDBTable);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    @Override
    public void undo() {
        diagram.getDbChildren().remove(newDBTable);
    }

}
